<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if ($route->POST('action') == 'add')
{
	$section = UV::readInt($route->POST("section"));
	$title = UV::readMQ($route->POST("title"));
	$link = translitIt(strip_tags($title));
	$i = 1;
	while ($db->getCell("select `link` from `tips` where `link` = '$link' and `ts_id` = '$section' limit 1")){
		$link = translitIt(strip_tags($title))."-".($i++);
	}
	$text = UV::readMQ($route->POST("text"));

	$query = "insert into `tips` values(NULL,'$section','$link','$title','$text')";
	
	$update = $db->sendQuery($query);
	if ($update){
		goToPage("/admin/tips/edit/".mysql_insert_id());
	} else {
		$errors[] = "Ошибка добавления записи";
	}
}

loadPage("head.php");
?>
<h1>Добавление статьи</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
?>
<form method='POST' id='edit'>
	<input type='hidden' name='action' value='add' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='section'>Раздел</label></td>
			<td width='600px'>
				<select name='section' class="double_short" id='section'><?php
					$sections = $db->getTable("select `id`,`name` from `tip_sections` order by `name`");
					echo select_create($sections,"id","name",$route->POST('section'));
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='title'>Заголовок</label></td>
			<td><input type='text' name='title' id='title' value='<?php echo $route->POST('title'); ?>' /></td>
		</tr>
		<tr>
			<td><label for='text'>Текст</label></td>
			<td><textarea name='text' id='text'><?php echo $route->POST('text'); ?></textarea></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Добавить' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>